
package com.ttomato.game.framework.math;

public class OverlapTester {
    public static boolean overlapCircles(Circle c1, Circle c2) {
        float distance = c1.mCenter.distSquared(c2.mCenter);
        float radiusSum = c1.mRadius + c2.mRadius;
        return distance < radiusSum * radiusSum;
    }

    public static boolean overlapRectangles(Rectangle r1, Rectangle r2) {
        if (r1.mLowerLeft.mX < r2.mLowerLeft.mX + r2.mWidth
                && r1.mLowerLeft.mX + r1.mWidth > r2.mLowerLeft.mX
                && r1.mLowerLeft.mY < r2.mLowerLeft.mY + r2.mHeight
                && r1.mLowerLeft.mY + r1.mHeight > r2.mLowerLeft.mY) {
            return true;
        }
        return false;
    }

    public static boolean overlapCircleRectangle(Circle circle, Rectangle rectangle) {
        float closestX = circle.mCenter.mX;
        float closestY = circle.mCenter.mY;

        if (closestX < rectangle.mLowerLeft.mX) {
            closestX = rectangle.mLowerLeft.mX;
        }

        if (closestX > rectangle.mLowerLeft.mX + rectangle.mWidth) {
            closestX = rectangle.mLowerLeft.mX + rectangle.mWidth;
        }

        if (closestY < rectangle.mLowerLeft.mY) {
            closestY = rectangle.mLowerLeft.mY;
        }

        if (closestY > rectangle.mLowerLeft.mY + rectangle.mHeight) {
            closestY = rectangle.mLowerLeft.mY + rectangle.mHeight;
        }

        return circle.mCenter.distSquared(closestX, closestY) < circle.mRadius * circle.mRadius;

    }

    public static boolean pointInCircle(Circle circle, Vector2 point) {
        return circle.mCenter.distSquared(point) <= circle.mRadius * circle.mRadius;
    }

    public static boolean pointInCircle(Circle circle, float x, float y) {
        return circle.mCenter.distSquared(x, y) <= circle.mRadius * circle.mRadius;
    }

    public static boolean pointInRectangle(Rectangle rectangle, Vector2 point) {
        return point.mX >= rectangle.mLowerLeft.mX
                && point.mX <= rectangle.mLowerLeft.mX + rectangle.mWidth
                && point.mY >= rectangle.mLowerLeft.mY
                && point.mY <= rectangle.mLowerLeft.mY + rectangle.mHeight;
    }

    public static boolean pointInRectangle(Rectangle rectangle, float x, float y) {
        return x >= rectangle.mLowerLeft.mX
                && x <= rectangle.mLowerLeft.mX + rectangle.mWidth
                && y >= rectangle.mLowerLeft.mY
                && y <= rectangle.mLowerLeft.mY + rectangle.mHeight;
    }
}
